<?php
/**
 * 《付款管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: In.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Payment extends FinanceUtil {
	public $tbl = Constant::tbl_cw_payment;
	
	public $iscustomer = array(
		'-' => '请选择',
		'0' => '否',
		'1' => '是',

		'9' => '其它',
	);
	
	public $type = array(
		'-' => '请选择',
		'1' => '货款',
		'2' => '宽带网络费',
		'3' => '行政杂费',
		'4' => '业务费',
	
		'5' => '借款',		
		'6' => '还款',
		'7' => '退款',
			
		'9' => '其它',
	);
	
	public $invoice = array(
		// '-' => '请选择',
		'1' => '普通收据',
		'2' => '普通发票',		
		'3' => '增值税票',
				
		'9' => '其它',
	);
	
	public $voucher = array(
		'-' => '请选择',
		'1' => '不需提供',
		'2' => '发送邮件',		
		'3' => '发送传真',
				
		'9' => '其它',
	);	
	
	public $urgent = array(
		'-' => '请选择',
		'1' => '立即支付',
		'2' => '三天之内',
		'3' => '七天之内',
		'4' => '当月之内',
		
		'9' => '其它',
	);
	
	public $bank = array(
		'-' => '请选择',
		'1' => '现金',
		'2' => '招商银行',
		'3' => '兴业银行',
		'4' => '中国民生银行',
		'5' => '中国建设银行',
		'6' => '中信银行',
		
		'7' => '支付宝',
		'8' => '微信支付',

		'9' => '其它',
	);
	
	public $account = array(
		'-' => '请选择',
		'1' => '个人',
		'2' => '公司',
	
		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待付款',
		'3' => '正在付款',
		'4' => '完成付款',
		// '5' => '已经做帐',
		'6' => '取消付款',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',	
		'supplier' => '供应商',
		'emp' => '负责人',
		'date' => '日期',

		'type' => '分类',
		'urgent' => '紧急程度',
		'invoice' => '发票',
		'payment' => '支付',
		'finish' => '完成时间',
		'amount' => '金额',
		'status' => '状态',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
			
		'office' => '办公区',
		'bank' => '银行',
		'account' => '帐户',
	
		'supplier' => '供应商',
		'emp' => '负责人',
		'type' => '分类',		

		'payment' => '方式',
		'currency' => '币种',
	
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('iscustomer_option', $this->iscustomer);
		
		$controller->assign_by_ref('type_option', $this->type);
		$controller->assign_by_ref('bank_option', $this->bank);
		
		$controller->assign_by_ref('invoice_option', $this->invoice);
		$controller->assign_by_ref('voucher_option', $this->voucher);
		$controller->assign_by_ref('urgent_option', $this->urgent);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'付款管理' => array('cc'=>'finance.php?do=Payment.find', 'js'=>'1'),
			'支出信息' => array('cc'=>'finance.php?do=Out.find', 'js'=>'1'),
			'支出明细' => array('cc'=>'finance.php?do=OutItem.find', 'js'=>'1'),
		
			'采购单' => array('cc'=>'material.php?do=Purchase.find', 'js'=>'1'),
			'外发加工' => array('cc'=>'mold.php?do=Process.find', 'js'=>''),
			'货运信息' => array('cc'=>'express.php?do=Plan.find', 'js'=>''),
			'进口业务' => array('cc'=>'express.php?do=In.find', 'js'=>''),			

			'银行帐号' => array('cc'=>'contact.php?do=SupplierBank.find', 'js'=>''),
			'借款信息' => array('cc'=>'finance.php?do=Borrow.find', 'js'=>'1'),
			'外币兑换' => array('cc'=>'finance.php?do=Convert.find', 'js'=>''),
				
			'供应商' => array('cc'=>'contact.php?do=Supplier.view_id', 'js'=>'1'),
					
			'统计分析' => array('cc'=>'finance.php?do=Payment.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'付款信息' => array('cc'=>'finance.php?do=Payment.group', 'js'=>''),
			'支出信息' => array('cc'=>'finance.php?do=Out.group', 'js'=>''),
			'支出明细' => array('cc'=>'finance.php?do=OutItem.group', 'js'=>''),
			
			'银行到帐' => array('cc'=>'finance.php?do=Transaction.group', 'js'=>''),
			'收入信息' => array('cc'=>'finance.php?do=In.group', 'js'=>''),
			'收入明细' => array('cc'=>'finance.php?do=InItem.group', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-3 month'));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '负责人',
			'oper' => '录入员',
			'cashier' => '出纳',
		);
	
		$controller->assign_by_ref('emp_option', $emp);
		
		$controller->assign('t1', strtotime("-2 month"));

		$controller->display($request, 'finance/payment/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}
		
		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'date') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['date'];
		}
		
		if ($px == 'finish') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['finish'];
		}
		
		if ($px == 'urgent') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $this->urgent[$row['urgent']];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$type_s = $request['type_s'];
		$name_s = $request['name_s'];
		
		$urgent_s = $request['urgent_s'];

		$bank_s = $request['bank_s'];
		$account_s = $request['account_s'];
				
		$amount1_s = $request['amount1_s'];
		$amount2_s = $request['amount2_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') { 
			$request['oper_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'cashier') {
			$request['cashier_s'] = $request['emp_a_s'];
		}
		
		$cashier_s = $this->_input($request['cashier_s']);

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		$where .= $this->_where_x("bank", $request['bank_s']);
		$where .= $this->_where_x("account", $request['account_s']);
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}
				
		$where .= $this->_where_x("invoice", $request['invoice_s']);
		
		$where .= $this->_where_x("payment", $request['payment_s']);
				
		if (!empty($urgent_s) && $urgent_s <> '-') {
			$where .= " AND urgent = '" . $urgent_s . "'";
		}
		
		if (!empty($amount1_s)) {
			$where .= " AND amount >= '" . $amount1_s . "'";
		}

		if (!empty($amount2_s)) {
			$where .= " AND amount < '" . $amount2_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND finish >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND finish < '" . $date2_s . "'";
		}
		
		if (!empty($cashier_s)) {
			$where .= " AND cashier = '" . $cashier_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		$emp = $_SESSION['id'];
		
		$p = array('1'=>'149', '2'=>'150', '3'=>'173');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
			
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}

		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}

		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		} else {
			$controller->clear_assign('result');
		}

		$controller->display($request, 'finance/payment/list');
		return true;
	}
	
	function _num($id) {
		$ret = $this->_out_item($id);
		
		return count($ret);
	}
	
	function _out_item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/OutItem.class.php');
		$aa = new OutItem();
		
		return $aa->_list_fk($id);
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s LIMIT 50";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'finance/payment/list');
		return true;
	}
	
	function xls_icbc($controller, $request) {
		$this->pretreat($controller);
		
		$request['tpl'] = "xls";

		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		
		$count = 0;
		$total = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
			
			if ($row['currency'] != 'CNY') {
				continue;
			}
			
			$row = $this->_view(null, $row);
			
			// $row['bank'] = $this->_bank("2", $row['supplier'], $row['payment_x']);
			if (!is_array($row['bank'])) {
				continue;
			}
			
			$row['transmit'] = $this->_transmit_type($row['bank']);
			if (empty($row['transmit'])) {
				continue;
			}
			
			$data[] = $row;
			
			$total += $row['amount'];
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $this->_my_bank($_SESSION['id'], 4));

		$controller->assign('total', $total);
		$controller->assign('count', $count);
		
		$filename = sprintf("icbc_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
				
		$controller->display($request, 'finance/payment/list_icbc_sxx');
		// $controller->display($request, 'finance/payment/list_icbc_3');
		return true;
	}
		
	function xls_cgb($controller, $request) {
		$this->pretreat($controller);
		
		$request['tpl'] = "xls";

		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);		
		$result = $db->query($sql);
		
		$count = 0;
		$total = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
			
			if ($row['currency'] != 'CNY') {
				continue;
			}
			
			$row = $this->_view(null, $row);
			
			// $row['bank'] = $this->_bank("2", $row['supplier'], $row['payment_x']);			
			if (!is_array($row['bank'])) {
				continue;
			}
			
			$row['transmit'] = $this->_transmit_type($row['bank']);
			if (empty($row['transmit'])) {
				continue;
			}
			
			$data[] = $row;
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $this->_my_bank($_SESSION['id'], 4, '广发银行'));

		$filename = sprintf("cgb_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
		
		$controller->display($request, 'finance/payment/list_cgb');
		
		return true;
	}
	
	function xls_pa($controller, $request) {
		$this->pretreat($controller);
		
		$request['tpl'] = "xls";

		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);		
		$result = $db->query($sql);
		
		$count = 0;
		$total = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
			
			if ($row['currency'] != 'CNY') {
				continue;
			}
			
			$row = $this->_view(null, $row);
			
			// $row['bank'] = $this->_bank("2", $row['supplier'], $row['payment_x']);			
			if (!is_array($row['bank'])) {
				continue;
			}
			
			$row['transmit'] = $this->_transmit_type($row['bank']);
			if (empty($row['transmit'])) {
				continue;
			}
			
			$data[] = $row;
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $this->_my_bank($_SESSION['id'], 4, '平安银行'));

		$filename = sprintf("pa_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
		
		$controller->display($request, 'finance/payment/list_pa');
		
		return true;
	}
	
	function xls_drc($controller, $request) {
		$this->pretreat($controller);
	
		$request['tpl'] = "xls";
	
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
			$this->findPost($controller, $request);
			return false;
		}
	
		$my_bank = $this->_my_bank($_SESSION['id'], 4, '东莞农村商业银行', '2');
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		$result = $db->query($sql);
	
		$count = 0;
		$total = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
				
			if ($row['currency'] != 'CNY') {
				continue;
			}
				
			$row = $this->_view(null, $row);
				
			if (!is_array($row['bank'])) {
				continue;
			}
				
			$row['drc'] = $this->_drc($row['bank'], $my_bank);
				
			$data[] = $row;
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		
		$controller->assign_by_ref('bank', $my_bank);
	
		$filename = sprintf("drc_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		$controller->display($request, 'finance/payment/list_drc');
	
		return true;
	}
	
	function bj($controller, $request) {
		$this->pretreat($controller);
	
		$request['tpl'] = "txt";
	
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
			$this->findPost($controller, $request);
			return false;
		}
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		$result = $db->query($sql);
	
		$count = 0;
		$total = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
				
			if ($row['currency'] != 'CNY') {
				continue;
			}
			
			$row = $this->_view(null, $row);
				
			// $row['bank'] = $this->_bank("2", $row['supplier'], $row['payment_x']);
			if (!is_array($row['bank'])) {
				continue;
			}
				
			$row['transmit'] = $this->_transmit_type($row['bank']);
			if (empty($row['transmit'])) {
				continue;
			}
			
			if ($row['bank']['type'] == '2') {
				$row['bank']['type'] = '0';
			} else {
				$row['bank']['type'] = '1';
			}
			
			if ($row['bank']['type'] == '1') {
				continue;
			}
			
			if ($row['bank']['bank'] == '北京银行') {
				$row['isother'] = '0';
			} else {
				$row['isother'] = '1';
			}
				
			$data[] = $row;
			
			$total += $row['amount'];
			$count++;
		}
		
		$bank = $this->_my_bank($_SESSION['id'], 4, '北京银行');
		
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $bank);
		
		$controller->assign('total', $total);
		$controller->assign('count', $count);
		
		// debug(print_r($data, true));
		// debug(print_r($bank, true));
		
		$content = $controller->sunxy($request, 'finance/payment/bj');
		
		// debug($content);
		
		// $request['tpl'] = "txt";
		
		$content = iconv("UTF-8", "GBK", $content);
		$filename = sprintf("bj_%s.plz", date("YmdHis"));
		header('Content-Disposition:text/xml;charset=gb2312;filename=' . $filename);
		
		$controller->assign_by_ref('content', $content);
		$controller->display($request, 'gbk');		
	
		// $controller->display($request, 'finance/payment/list_bj');
	
		return true;
	}
	
	function txt($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";
		
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		if ($request['bank'] == 'icbc') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '中国工商银行');
				
			$my_tpl = 'finance/payment/icbc';
			$filename = sprintf("%s.bpt", date("YmdHis"));
		} elseif ($request['bank'] == 'cmb') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '招商银行', '2');
				
			$my_tpl = 'finance/payment/cmb';
			$filename = sprintf("cmb-%s.txt", date("YmdHis"));
		} elseif ($request['bank'] == 'citic') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '中信银行');
				
			$my_tpl = 'finance/payment/citic';
			$filename = sprintf("citic-%s.txt", date("YmdHis"));
		} elseif ($request['bank'] == 'cmbc') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '中国民生银行', '2');
				
			$my_tpl = 'finance/payment/list_cmbc';
			$filename = sprintf("cmbc-%s.txt", date("YmdHis"));
		} elseif ($request['bank'] == 'drc') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '东莞农村商业银行', '2');
				
			$my_tpl = 'finance/payment/list_drc';
			$filename = sprintf("cmbc-%s.txt", date("YmdHis"));
		} elseif ($request['bank'] == 'boc') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '中国银行', '2');
				
			$my_tpl = 'finance/payment/boc';
			$filename = sprintf("boc-%s.txt", date("YmdHis"));
		} elseif ($request['bank'] == 'ccb') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '中国建设银行', '2');
				
			$my_tpl = 'finance/payment/ccb';
			$filename = sprintf("ccb-%s.txt", date("YmdHis"));
		} elseif ($request['bank'] == 'cib') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '兴业银行', '1');
				
			$my_tpl = 'finance/payment/cib';
			$filename = sprintf("cib-%s.txt", date("YmdHis"));
		} elseif ($request['bank'] == 'cib_c') {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '兴业银行', '2');
				
			$my_tpl = 'finance/payment/cib_c';
			$filename = sprintf("cib_c-%s.txt", date("YmdHis"));
		} else {
			$my_bank = $this->_my_bank($_SESSION['id'], 4, '招商银行');
				
			$my_tpl = 'finance/payment/list';
			$filename = sprintf("BATCH-%s.PAY", date("YmdHis"));
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		
		$count = 0;
		$total = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
			
			if ($row['currency'] != 'CNY') {
				continue;
			}
			
			$row = $this->_view(null, $row);
			
			if (!is_array($row['bank'])) {
				continue;
			}
			
			$row['transmit'] = $this->_transmit_type($row['bank'], $my_bank['zone'], $my_bank['city']);
			if (empty($row['transmit'])) {
				continue;
			}
			
			$data[] = $row;
			
			$total += $row['amount'];
			$count++;
		}
		$controller->assign_by_ref('result', $data);
		
		$controller->assign('total', $total);
		$controller->assign('count', $count);
		
		$controller->assign_by_ref('bank', $my_bank);
		$content = $controller->sunxy($request, $my_tpl);
		
		$content = iconv("UTF-8", "GBK", trim($content));
		header('Content-Disposition:text/vcf; filename=' . $filename);
		
		$controller->assign_by_ref('content', $content);
		$controller->display($request, 'gbk');
		return true;
	}
	
	function _batch_insql($request) {		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		return $insql;
	}

	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}
		
		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('149') || $this->_audit('150')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'finance/payment/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$iscustomer = $request['iscustomer'];
		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);

		$type = $request['type'];
		$name = addslashes($request['name']);
		$amount = $request['amount'];
		$currency = $request['currency'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$invoice = $request['invoice'];
		$urgent = $request['urgent'];
		$payment = $request['payment'];		
		$payment_x = addslashes($request['payment_x']);
		$this->_update_payment($payment, "2", $supplier, $request['payment_x']);
		// $supplier = $this->_update_x($supplier, $request['supplier_x'], $payment, $request['payment_x']);
		
		$voucher = $request['voucher'];
		$voucher_x = $request['voucher_x'];
		
		$content = addslashes($request['content']);
		
		$date = $request['date'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, iscustomer, supplier, supplier_x, date, type, name, amount, currency, rate, rmb, invoice, urgent, payment, payment_x, voucher, voucher_x, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $iscustomer, $supplier, $supplier_x, $date, $type, $name, $amount, $currency, $rate, $rmb, $invoice, $urgent, $payment, $payment_x, $voucher, $voucher_x, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_x($supplier, $supplier_x, $payment, $payment_x) {
		require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');
		$aa = new Supplier();
		
		return $aa->_update_x($supplier, $supplier_x, $payment, $payment_x);
	}

	function _update_payment($payment, $info, $info_id, $payment_x) {		
		if ($payment == '1' || $payment == '3') {
			$this->_update_linkman($info, $info_id, $payment_x);
		}
		
		if ($payment == '2' || $payment == '4') {
			$this->_update_bank($info, $info_id, $payment_x);
		}
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/payment/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$iscustomer = $request['iscustomer'];
		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);

		$type = $request['type'];
		$name = addslashes($request['name']);
		$amount = $request['amount'];
		$currency = $request['currency'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$invoice = $request['invoice'];
		$urgent = $request['urgent'];
		$payment = $request['payment'];
		$payment_x = addslashes($request['payment_x']);
		$this->_update_payment($payment, "2", $supplier, $request['payment_x']);
		// $supplier = $this->_update_x($supplier, $request['supplier_x'], $payment, $request['payment_x']);
		
		$voucher = $request['voucher'];
		$voucher_x = $request['voucher_x'];
		
		$content = addslashes($request['content']);
		
		$date = $request['date'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', iscustomer = '%s', supplier = '%s', supplier_x = '%s', date = '%s', type = '%s', name = '%s', amount = '%s', currency = '%s', rate = '%s', rmb = '%s', invoice = '%s', urgent = '%s', ";
		$fmt .= "payment = '%s', payment_x = '%s', voucher = '%s', voucher_x = '%s', content = '%s', emp = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $iscustomer, $supplier, $supplier_x, $date, $type, $name, $amount, $currency, $rate, $rmb, $invoice, $urgent, $payment, $payment_x, $voucher, $voucher_x, $content, $emp, $id);
		debug($sql);
		$db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/payment/upload');
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/payment/copy');
		return true;
	}
	
	function batch_type($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'finance/payment/batch_type');
		return true;
	}
	
	function batch_typePost($controller, $request) {
		$this->pretreat($controller);
		
		$content = addslashes($request['content']);
		$type = $request['type'];

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request['cc'])) {
			$fmt = "UPDATE %s SET type = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $type, $id);
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->invoice = $this->_shift($this->invoice);
		$this->voucher = $this->_shift($this->voucher);
		$this->payment = $this->_shift($this->payment);
		$this->urgent = $this->_shift($this->urgent);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);

		$controller->assign_by_ref('result', $row);

		if (!empty($request['print'])) {
			$controller->display($request, 'finance/payment/view_print');
		} elseif ($request['check'] == 'hsbc') {
			$controller->display($request, 'finance/payment/view_hsbc');
		} elseif ($request['check'] == 'icbc') {
			$controller->display($request, 'finance/payment/view_icbc');
		} else {
			$row['item'] = $this->_fk_item($row['id']);
			
			$controller->display($request, 'finance/payment/view');
		}
		
		return true;
	}
	
	function _fk_item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/OutItem.class.php');
		$aa = new OutItem();
		
		return $aa->_list_fk($id);
	}
	
	function _view($id = 0, $row = null, $cache = true) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
				
		$row['urgent_t'] = $this->urgent[$row['urgent']];
		$row['invoice_t'] = $this->invoice[$row['invoice']];
		
		$row['account_t'] = $this->account[$row['account']];
		$row['payment_t'] = $this->payment[$row['payment']];				
		$row['payment_z'] = $this->_payment_z($row['payment'], $row['bank'], $row['account']);
		
		if ($row['iscustomer'] == '1') {
			$row['bank'] = $this->_bank("3", $row['supplier'], $row['payment_x']);
		} else {
			$row['bank'] = $this->_bank("2", $row['supplier'], $row['payment_x']);
		}
		
		$row['payment_x'] = json_decode($row['payment_x']);
		$row['voucher_t'] = $this->voucher[$row['voucher']];
		$row['cashier_t'] = Name::emp($row['cashier']);
		
		$row = $this->_view_t($row);
		
		if ($row['iscustomer']) {
			$row['supplier_t'] = Name::customer_code($row['supplier']);
			$row['supplier_x'] = json_decode($row['supplier_x']);
		} else {
			$row['supplier_x'] = json_decode($row['supplier_x']);
		}

		return $row;
	}
	
	function _payment_z($a, $b, $c) {
		if ($a == '1' || $a == '9' || empty($b)) {
			return $this->payment[$a];
		}
		
		$ret = sprintf('%s[%s,%s]', $this->payment[$a], $this->bank[$b], $this->account[$c]);
		
		return $ret;
	}
	
	function _rmb($s) {
		$ret = '';
		
		$digits = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
		
		for ($i = 0; $i < strlen($s); $i++) {
			$k = substr($s, $i, 1);
			if (!is_numeric($k)) {
				continue;
			}
			
			$ret .= $digits[$k] . "　";
		}
		
		return $ret;
	}
	
	function tj1($controller, $request) {
		$a = array(
			'南山付款'=>array('a'=>'324', 'office'=>'1'),
			'沙井付款'=>array('a'=>'325', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->tj($controller, $request);
		}
	}
	
	function tj2($controller, $request) {
		$a = array(
			'南山付款（周报）'=>array('a'=>'324', 'office'=>'1'),
			'沙井付款（周报）'=>array('a'=>'325', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
			
			$this->tjw($controller, $request);
		}				
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$cc = array(
			'完成付款'=>array('w' => "office = '$office' AND DATE_FORMAT(finish, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'currency, status'),	
			'新增信息'=>array('w' => "office = '$office' AND DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'currency, status'),
			'更新信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'currency, status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'finance/payment/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$cc = array(
			'完成付款'=>array('w' => "office = '$office' AND DATE_FORMAT(finish, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'currency, status'),	
			'新增信息'=>array('w' => "office = '$office' AND DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'currency, status'),
			'更新信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'currency, status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);

		$body = $this->_tj_html($controller, $request, 'finance/payment/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'currency';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, SUM(amount*rate) AS c, SUM(amount) AS d FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, SUM(amount*rate) AS c, SUM(amount) AS d FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));
		
		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'finance/payment/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}
	
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(amount) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'bank') {
			$row['a'] = $this->bank[$row['a']];
		} elseif ($px == 'account') {
			$row['a'] = $this->account[$row['a']];
		} elseif ($px == 'urgent') {
			$row['a'] = $this->urgent[$row['a']];
		} elseif ($px == 'invoice') {
			$row['a'] = $this->invoice[$row['a']];
		} elseif ($px == 'payment') {
			$row['a'] = $this->payment[$row['a']];
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function verify($controller, $request) {
		$this->invoice = $this->_shift($this->invoice);
		$this->voucher = $this->_shift($this->voucher);
		$this->payment = $this->_shift($this->payment);
		$this->urgent = $this->_shift($this->urgent);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign('bank', $this->_bank22($row));
		
		$controller->display($request, 'finance/payment/verify');
		return true;
	}
	
	function _bank22($row) {
		$payment = $row['payment'];
		$bank = $row['bank'];
		$account = $row['account'];
		
		$currency = $row['currency'];
		
		if (!empty($bank) && $bank <> '-') {
			return $bank;
		}
		
		if ($currency == 'HKD' || $currency == 'USD' || $currency == 'EUR' || $currency == 'AUD') {
			return 2;
		}
		
		if ($payment == '1' || $payment == '9') {
			return 1;
		}
		
		if ($payment == '2' || $payment == '3' || $payment == '4') {
			$invoice = $row['invoice'];
			
			if ($invoice == '2' || $invoice == '3') {
				$office = $row['office'];
				if ($office == '1') {
					return 5;
				} 
				
				return 3;
			}
			
			return 5;
		}
		
		return 5;
	}

	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$bank = $request['bank'];
		$account = $request['account'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET bank = '%s', account = '%s', content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $bank, $account, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_verify($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$a = $this->_bank22($row);
			$ret[$a]++;
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		asort($ret);
		$key = array_keys($ret);
		
		$bank = array_pop($key);
		$controller->assign('bank', $bank);
		
		$controller->display($request, 'finance/payment/batch_verify');
		return true;
	}
	
	function _batch_insql222($a) {
		// $a = json_decode($cc);		
		if (!$a || $a == NULL) {
			debug("cc is NULL");
			return '';
		}
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($a)) {
			$insql .= "', '" . $v;
		}
		$insql .= "')";
		
		return $insql;
	}
	
	function batch_verifyPost($controller, $request) {
		$this->pretreat($controller);
		
		$bank = $request['bank'];
		$account = $request['account'];
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request['cc'])) {
			$fmt = "UPDATE %s SET account = '%s', content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $account, $content, $status, $id);
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_begin($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$a = $this->_bank22($row);
			$ret[$a]++;
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		asort($ret);
		$key = array_keys($ret);
		
		$bank = array_pop($key);
		$controller->assign('bank', $bank);

		$controller->display($request, 'finance/payment/batch_begin');
		return true;
	}
	
	function batch_finish($controller, $request) {
		$this->pretreat($controller);
		
		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$a = $this->_bank22($row);
			$ret[$a]++;
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		asort($ret);
		$key = array_keys($ret);
		
		$bank = array_pop($key);
		$controller->assign('bank', $bank);

		$controller->display($request, 'finance/payment/batch_finish');
		return true;
	}
	
	function batch_beginPost($controller, $request) {
		return $this->batch_verifyPost($controller, $request);
	}
	
	function batch_finishPost($controller, $request) {
		$this->pretreat($controller);
		
		while (list($k, $id) = each($request['cc'])) {
			$this->_finish22($controller, $request, $id);
		}		

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _finish22($controller, $request, $id) {
		$this->invoice = $this->_shift($this->invoice);
		$this->voucher = $this->_shift($this->voucher);
		$this->payment = $this->_shift($this->payment);
		$this->urgent = $this->_shift($this->urgent);
		
		$finish = $request['finish'];
		$bank = $request['bank'];
		$account = $request['account'];
		
		$email = $request['email'];
		$content = addslashes($request['content']);
		$status = 4;
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET finish = '%s', bank = '%s', account = '%s', content = concat(content, '%s'), cashier = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $finish, $bank, $account, $content, $_SESSION['id'], $status, $id);
		debug($sql);
		$db->query($sql);
		
		$this->_out_item_finish($id, $finish);
				
		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		if ($email == '1') {			
			$from = $_SESSION['id'];
	
			$to = $row['emp'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('付款 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'finance/payment/body_5');
			// debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
			if ($row['emp'] <> $row['oper']) {
				$this->_notice($from, $row['oper'], $subject, $body, 1);
			}
			
			if ($row['voucher'] == '2') {
				$this->mail_html($_SESSION['emp']['email'], $row['voucher_x'], $subject, $body);
			}
		}
	}
	
	function _out_item_finish($id, $finish) {
		require_once(dirname(__FILE__) . '/OutItem.class.php');
		$aa = new OutItem();
		
		$aa->_update_finish($id, $finish);
	}

	function finish($controller, $request) {
		$this->invoice = $this->_shift($this->invoice);
		$this->voucher = $this->_shift($this->voucher);
		$this->payment = $this->_shift($this->payment);
		$this->urgent = $this->_shift($this->urgent);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		if (empty($row['finish'])) {
			$row['finish'] = time();
		}

		$controller->assign_by_ref('result', $row);
		
		$controller->assign('bank', $this->_bank22($row));
		
		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);

		$controller->display($request, 'finance/payment/finish');
		return true;
	}

	function finishPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$this->_finish22($controller, $request, $id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function begin($controller, $request) {
		$this->invoice = $this->_shift($this->invoice);
		$this->voucher = $this->_shift($this->voucher);
		$this->payment = $this->_shift($this->payment);
		$this->urgent = $this->_shift($this->urgent);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		if (empty($row['finish'])) {
			$row['finish'] = time();
		}

		$controller->assign_by_ref('result', $row);
		
		$controller->assign('bank', $this->_bank22($row));
		
		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);

		$controller->display($request, 'finance/payment/begin');
		return true;
	}
	
	function beginPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$this->_begin22($controller, $request, $id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _begin22($controller, $request, $id) {
		$this->invoice = $this->_shift($this->invoice);
		$this->voucher = $this->_shift($this->voucher);
		$this->payment = $this->_shift($this->payment);
		$this->urgent = $this->_shift($this->urgent);
		
		$account = $request['account'];
		$email = $request['email'];
		$content = addslashes($request['content']);
		$status = 3;
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET account = '%s', content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $account, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		
		// $this->_out_item_finish($id, $finish);
				
		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		if ($email == '1') {			
			$from = $_SESSION['id'];
	
			$to = $row['emp'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('付款 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'finance/payment/body_5');
			// debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
			if ($row['emp'] <> $row['oper']) {
				$this->_notice($from, $row['oper'], $subject, $body, 1);
			}
		}
	}
	
	function money($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);
		
		$controller->assign('t1', strtotime('-3 day'));

		$controller->display($request, 'finance/payment/money');
		return true;
	}
	
	function moneyPost($controller, $request) {
		require_once(dirname(__FILE__) . '/Money.class.php');
		$money = new Money();
		
		$money->addPost($controller, $request);
		
		$id = $request['id'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '5', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
		return true;
	}	
	
	function _test2() {
		$ret = array();
		if ($this->_audit('149')) {
			$ret[] = '1';
		}
		
		if ($this->_audit('150')) {
			$ret[] = '2';
		}
		
		if ($this->_audit('173')) {
			$ret[] = '3';
		}
		
		$ret[] = '1';
		$ret[] = '2';
		// $ret[] = '3';
		
		return $ret;
	}
        
	function t($controller, $request) {	

		$a = $this->_test2();
		
		if (count($a) > 0) {
			print_r($a);
			
			$s = implode(",", $a);
			echo $s;
			
		} else {
			echo "2";
		}
				
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE supplier_x is NULL";
		$fmt = "SELECT * FROM %s WHERE length(supplier_x) < 10";
		$fmt = "SELECT * FROM %s WHERE createtime < '2011-05-09'";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$supplier_x = $this->_supplier_x($row['supplier']);			
			
			$fmt = "UPDATE %s SET supplier_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $supplier_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function x($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s ORDER BY id DESC";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$supplier_x = $this->_x($row['supplier'], $row['supplier_x']);
			
			$fmt = "UPDATE %s SET supplier_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $supplier_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _x($id, $supplier_x) {
		$j = json_decode($supplier_x);
		
		// var_dump($j);
		
		require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');
		$aa = new Supplier();
		
		$c = $aa->_view($id);
				
		$ret['name'] = $c['name'];
		$ret['fullname'] = $c['fullname'];
		$ret['address'] = $c['address'];
		$ret['web'] = $c['url'];
		
		if (empty($j->contact)) {
			$ret['contact'] = $j->name;
		} else {
			$ret['contact'] = $j->contact;
		}
		
		$ret['mobile'] = $j->mobile;
		$ret['tel'] = $j->tel;
		$ret['fax'] = $j->fax;
		$ret['email'] = $j->email;		
		$ret['content'] = $j->content;
		
		$ret = json_encode($ret);
		
		return addslashes($ret);
	}
	
	function _list_2($supplier) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE supplier = '%s' AND status < 4 ORDER BY id DESC LIMIT 10";
		// $fmt = "SELECT * FROM %s WHERE supplier = '%s' ORDER BY id DESC LIMIT 10";
		$sql = sprintf($fmt, $this->tbl, $supplier);
		debug($sql);
		$result = $db->query($sql);
		$ret = array("-"=>"请选择");
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
						
			$ret[$row['id']] = sprintf("%s [%s,%s,%s,%s]", $row['id'], $row['supplier_t'], $row['amount'], date('Y-m-d', strtotime($row['createtime'])), $row['status_t']);
			// $ret[] = $row;
		}
		
		return $ret;
	}
		
	function _tj_a($controller, $request) {	
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, SUM(amount) as c FROM %s a WHERE createtime >= %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		$this->_group_tj($controller, $sql);

		return $controller->sunxy($request, 'tj_a');
	}
	
	function star($controller, $request) {
		
		$s = "DATE_FORMAT(finish, '%Y-%m-%d') > DATE_FORMAT(DATE_ADD(now(), INTERVAL -180 DAY), '%Y-%m-%d')";
		
		require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');
		$aa = new Supplier();
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT supplier AS a, SUM(rmb) AS c FROM %s WHERE %s GROUP BY supplier ORDER BY c DESC";
		$sql = sprintf($fmt, $this->tbl, $s);
		echo $sql . "\n";
		$result = $db->query($sql);
		$i = 1;
		while ($row = $db->fetch_array($result)) {
			$star = $this->_star($i++, $row['c']);
			
			$aa->_star($row['a'], $star);
		}
		
		logdate($controller, $request);
		return true;
	}
	
	function _star($i, $c) {
		if ($i < 10) {
			return 5;
		}
		
		if ($i < 20) {
			return 4;
		}
		
		if ($i < 30) {
			return 3;
		}
		
		if ($i < 40) {
			return 2;
		}
		
		return 1;
	}
	
	function supplier($controller, $request) {
		$this->pretreat($controller);
	
		$dd = date('Y-m-d', strtotime("-12 MONTH"));
		
		$s = "DATE_FORMAT(createtime, '%Y-%m-%d') > DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')";
	
		require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');
		$aa = new Supplier();
		$aa->_update_status_x("1", "2", $dd);
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT supplier AS a, SUM(rmb) AS c FROM %s WHERE %s GROUP BY supplier ORDER BY c DESC";
		$sql = sprintf($fmt, $this->tbl, $s);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$aa->_update_status($row['a'], '1');
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s' AND status < 4";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
	
	function _supplier($supplier) {
		$dd = date('Y-m-d', strtotime("-12 MONTH"));
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE supplier = '%s' AND createtime > '%s'";
		$sql = sprintf($fmt, $this->tbl, $supplier, $dd);
		echo $sql . "\n";
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		if ($row['t'] > 0) {
			return true;
		}
		
		return false;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'finance/payment/_list_c');
		return true;
	}
	
	function view_supplier($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_supplier($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'finance/payment/_list_c');
		return true;
	}
}
?>
